<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/5/6
 * Time: 12:39
 */

namespace app\alipay\model;

use app\account\model\BonusPointManager;
use app\account\model\GradeManager;
use app\account\model\UserManager;
use app\common\model\Base;
use app\shopping\model\OrderManager;
use think\Config;
use think\Log;

class ConsumeManager extends UserManager{

    private $order;

    const DB_NAME="user";

    const COST_ADD=1;
    const COST_PLUS=2;
    const COST_CLEAR=3;



    public function __construct()
    {
        parent::__construct();
        Config::load(APP_PATH . 'alipay/config.php');

        $this->order = new OrderManager();
    }


    /*
     * uid 账户
     * totoal_fee:金额
     */
    public function updateConsumeRecord($uid,$total_fee){
        //更新用户消费记录
        $result = $this->updateUserCost($uid, $total_fee);

        if(!$result){
            return false;
        }

        //更新推荐人的积分
        $referrer = $this->getRefereeByUid($uid);
        $bonusPointManager = new BonusPointManager();
        foreach ($referrer as $key=>$value){
            $referrerUid = $referrer[$key]["uid"];
            $bonusPoint = $referrer[$key]["discount"]*$total_fee;

            $bonusPointManager->setBonusPointGain($referrerUid, $bonusPoint, $uid);
            $this->updateUserBonusPoint($referrerUid, $bonusPoint);
        }

        return $result;
    }


    /*
     * @param uid用户的编号
     */
    public function queryConSumeRecord($uid, $mount=5){

        return $this->updateUserBonusPoint($uid,1);
    }



    public function updateUserBonusPoint($uid,$totalBonusPoint,$mode=self::COST_ADD){
        $oldBonusPoint = $this->getConsumeBonusPointByUid($uid);

        if($mode == self::COST_ADD){
            $newBonusPoint = $oldBonusPoint + $totalBonusPoint;
        }else if($mode == self::COST_PLUS){
            if($oldBonusPoint < $totalBonusPoint){
                return false;
            }
            $newBonusPoint = $oldBonusPoint - $totalBonusPoint;
        }

        return  $this->updateUserByCustom( array("uid"=>$uid), array("bonus_point"=>$newBonusPoint));
    }

    /*
     * mode : 表示总花费改变模式，默认为增加
     */
    private function updateUserCost($uid,$total_fee,$mode=self::COST_ADD){
        $oldCost = $this->getConsumeCostByUid($uid);

        if($mode == self::COST_ADD){
            $newCost = $oldCost + $total_fee;

        }else if($mode == self::COST_PLUS){
            if($oldCost < $total_fee){
                return false;
            }
            $newCost = $oldCost - $total_fee;
        }

        $this->updateUserByCustom( array("uid"=>$uid), array("total_cost"=>$newCost));

        $gradeManager = new GradeManager();
        $result = $gradeManager->isGrade($uid);

        return true;
    }

    public function getUserGradeByUid($uid){
        return $this->getUserByCustom(array("uid"=>$uid))["agency_grade_id"];
    }

    public function getConsumeLevelByUid($uid){

    }

    public function getConsumeBonusPointByUid($uid){
        return $this->getUserByCustom(array("uid"=>$uid))["bonus_point"];
    }

    public function getConsumeCostByUid($uid){
        return $this->getUserByCustom(array("uid"=>$uid))["total_cost"];
    }
    /*
     * 获取消费信息
     */

    /*
     * 三级分销获取推荐人
     * referee : 推荐人
     * refereeLevel:几级推荐人，默认为三级
     */
    private function getRefereeByUid($uid, $refereeLevel=3){
        $discount = config('Consume.discount');

        if($refereeLevel > 3 && $refereeLevel < 0)
            return false;

        for($i=0; $i<$refereeLevel; $i++){
            $refereeSql = "select uid from agency_user_subordinate where subordinate_id='$uid'";
            $uid = $this->mysql->selectSQLRow($refereeSql)["uid"];
            $refereeUid[$i] = $uid;
        }


        foreach ($refereeUid as $key=>$value){
            $refereeIncome[$key] = array(
                "uid"=>$refereeUid[$key],
                "discount"=>$discount[$key]
            );
        }
        return $refereeIncome;
    }
}